<?php

class Peringkat_qualifiedController extends AdminController {

    public $db;

    public function actionView() {
        $sort = new CSort;
        $sort->attributes = array(
//            'reward_node_right' => array(
//                'asc' => 'reward_node_right',
//                'desc' => 'reward_node_right DESC',
//            ),
            'leadership_level_log_periode' => array(
                'asc' => 'leadership_level_log_periode',
                'desc' => 'leadership_level_log_periode DESC',
            ),
            'network_mid' => array(
                'asc' => 'network_mid',
                'desc' => 'network_mid DESC',
            ),
        );


        $data_arr = array(
            'select' => '*',
            'from' => 'sys_leadership_level_log sr',
            'join' => array(
                'sn' => 'sys_network sn',
                'sm' => 'sys_member sm',
            ),
            'params_join' => array(
                'sn' => 'sr.leadership_level_log_network_id=sn.network_id',
                'sm' => 'sn.network_id=sm.member_network_id',
            ),
            'order' => 'leadership_level_log_periode DESC',
//            'where'=>'reward_is_qualified=:qualified',
//            'params'=>array(':qualified'=>'1'),
        );

        $results = $this->get_data($data_arr);
        $dataProvider = new CArrayDataProvider($results,
                        array(
                            'keyField' => 'network_id',
                            'sort' => $sort,
                ));

        $this->render('view', array(
            'dataProvider' => $dataProvider,
        ));
    }

    private function get_data($data_arr) {
        if (!isset($this->db)) {
            $this->db = Yii::app()->db->createCommand();
        }
        $this->db->select($data_arr['select']);
        $this->db->from($data_arr['from']);
        if ($data_arr['where'] != '') {
            $this->db->where($data_arr['where'], $data_arr['params']);
        }
        //  array('not in','network_level_status_log_value','passive'),
        // array('>=','date(network_level_status_log_datetime)',$start_daily)
        if (isset($data_arr['join'])) {
            foreach ($data_arr['join'] AS $key => $val) {

                $this->db->join($val, $data_arr['params_join'][$key]);
            }
        }

        if (isset($data_arr['group']) AND trim($data_arr['group']) != '') {
            $this->db->group($data_arr['group']);
        }
        if (isset($data_arr['order']) AND trim($data_arr['order']) != '') {
            $this->db->order($data_arr['order']);
        }

        return $this->db->queryAll();
    }

    public function actionReport() {
        $this->layout = '//layouts/empty';
        $mid = function_lib::uri_segment(5);
        $tanggal = function_lib::uri_segment(6);
        $network_id = function_lib::get_network_id($mid);

        //cari data
        $sql_omset_pribadi = "SELECT leadership_monthly_turnover_value
                                    FROM sys_leadership_monthly_turnover
                                    WHERE leadership_monthly_turnover_network_id = '$network_id'
                                    AND leadership_monthly_turnover_sponsored_network_id = '$network_id'
                                    AND leadership_monthly_turnover_periode = '" . $tanggal . "'
                                    ";
        $omset_belanja_pribadi = Yii::app()->db->createCommand($sql_omset_pribadi)->queryScalar();

        //cari omset sponsornya 2 peringkat diatas
        $sql_peringkat_omset_group = "SELECT leadership_monthly_turnover_value
                                            FROM sys_leadership_monthly_turnover
                                            WHERE leadership_monthly_turnover_network_id = '$network_id'
                                            AND leadership_monthly_turnover_sponsored_network_id != '$network_id'
                                            AND leadership_monthly_turnover_periode = '" . $tanggal . "'
                                            ORDER BY leadership_monthly_turnover_value DESC ";

        $result_omset_group = Yii::app()->db->createCommand($sql_peringkat_omset_group)->queryAll();
        
        $data['mid'] = $mid;
        $data['periode'] = $tanggal;
        $data['nama'] = function_lib::get_member_name($network_id);
        
        $this->render('report', array(
            'omset_pribadi' => $omset_belanja_pribadi,
            'omset_group' => $result_omset_group,
            'data' => $data,
        ));
    }

}

?>
